package cn.edu.zhku.service.impl;

import cn.edu.zhku.dto.TaskDto;
import cn.edu.zhku.dto.UserDto;
import cn.edu.zhku.mapper.form.FormMapper;
import cn.edu.zhku.pojo.Group;
import cn.edu.zhku.pojo.Task;
import cn.edu.zhku.pojo.User;
import cn.edu.zhku.mapper.GroupMapper;
import cn.edu.zhku.service.GroupService;
import cn.edu.zhku.util.InviteCodeGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class GroupServiceImpl implements GroupService {

    @Autowired
    private GroupMapper groupMapper;
    @Autowired
    private FormMapper formMapper;

    @Override
    @Transactional
    public void addOne(Group group) {
        group.setInviteCode(InviteCodeGenerator.generate(group.getUserId()));
        this.groupMapper.insertOne(group);
    }

    @Override
    @Transactional
    public void addOne2UserGroup(Integer userId, String inviteCode, Date joinTime) {
        this.groupMapper.insertOne2UserGroup(userId, inviteCode, joinTime);
    }

    @Override
    @Transactional
    public void modifyOne(Group group) {
        this.groupMapper.updateOne(group);
    }

    @Override
    @Transactional
    public void removeGroupBatch(List<Integer> groupIds) {
        this.groupMapper.deleteGroupBatch(groupIds);
        this.groupMapper.deleteUserGroupBatch(groupIds);
        this.groupMapper.deleteTaskGroupBatch(groupIds);
        this.formMapper.deleteByGroupIdBatch(groupIds);
    }

    @Override
    @Transactional
    public void removeUserBatch(Integer groupId, List<Integer> userIds) {
        this.groupMapper.deleteUserBatch(groupId, userIds);
    }

    @Override
    @Transactional
    public void removeTaskBatch(Integer groupId, List<Integer> taskIds) {
        this.groupMapper.deleteTaskBatch(groupId, taskIds);
    }

    @Override
    public Group findById(Integer groupId) {
        return this.groupMapper.selectById(groupId);
    }

    @Override
    public boolean checkGroupExistByGroupIdBatch(List<Integer> groupIds) {
        return this.groupMapper.selectGroupNumByGroupIdBatch(groupIds) == groupIds.size();
    }

    @Override
    public Group findByInviteCode(String inviteCode) {
        return this.groupMapper.selectByInviteCode(inviteCode);
    }

    @Override
    public boolean checkGroupExistByStuIdAndInviteCode(Integer userId, String inviteCode) {
        return this.groupMapper.selectGroupNumByStuIdAndInviteCode(userId, inviteCode) == 1;
    }

    @Override
    public boolean checkGroupUserExistByGroupIdAndUserIdBatch(Integer groupId, List<Integer> userIds) {
        return this.groupMapper.selectGroupUserNumByGroupIdAndUserIdBatch(groupId, userIds) == userIds.size();
    }

    @Override
    public boolean checkGroupTaskExistByGroupIdAndTaskIdBatch(Integer groupId, List<Integer> taskIds) {
        return this.groupMapper.selectGroupTaskNumByGroupIdAndTaskIdBatch(groupId, taskIds) == taskIds.size();
    }

    @Override
    public List<Group> findGroupTea(Group group) {
        return this.groupMapper.selectGroupTea(group);
    }

    @Override
    public List<Group> findGroupStu(Group group) {
        return this.groupMapper.selectGroupStu(group);
    }

    @Override
    public List<User> findGroupUser(UserDto userDto) {
        return this.groupMapper.selectGroupUser(userDto);
    }

    @Override
    public List<Task> findGroupTaskTea(TaskDto taskDto) {
        return this.groupMapper.selectGroupTaskTea(taskDto);
    }

    @Override
    public List<Task> findGroupTaskStu(TaskDto taskDto) {
        return this.groupMapper.selectGroupTaskStu(taskDto);
    }

    @Override
    public Integer getGroupUserNumByGroupId(Integer groupId) {
        return this.groupMapper.selectGroupUserNumByGroupId(groupId);
    }

    @Override
    public Integer getGroupTaskNumByGroupId(Integer groupId) {
        return this.groupMapper.selectGroupTaskNumByGroupId(groupId);
    }

    @Override
    public Integer getGroupTaskFormNumByGroupIdAndTaskId(Integer groupId, Integer taskId) {
        return this.groupMapper.selectGroupTaskFormNumByGroupIdAndTaskId(groupId, taskId);
    }

}
